package com.example.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class CustomViews extends View {
	private Paint paint;
	private Rect rect;
	private int count;
	private int radius = 100;

	public CustomViews(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		rect = new Rect();

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					if (count >= 10) {
						count = 0;
					}
					count++;

					try {
						Thread.sleep(1000);

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					postInvalidate();
				}
			}
		}).start();
	}

	public CustomViews(Context context) {
		super(context);
	}

	public CustomViews(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//画一个圆形
		paint.setColor(Color.YELLOW);
		canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);

		paint.setColor(Color.RED);
		paint.setTextSize(50);
		String counts = String.valueOf(count);
		// 处理字体的宽高
		paint.getTextBounds(count + "", 0, counts.length(), rect);
		float textwidth = rect.width();
		float textheight = rect.height();
		canvas.drawText(count + "", (getWidth() - textwidth) / 2,
				(getHeight() + textheight) / 2, paint);

	}

}
